package com.tsky.yyj.json;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.json.JSONException;
import org.apache.struts2.json.JSONUtil;

import com.tsky.casing.QueryBean;
import com.tsky.common.BeanUtil;
import com.tsky.system.entity.Constant;
import com.tsky.system.entity.Role;
import com.tsky.yyj.entity.Agent;
import com.tsky.yyj.entity.Area;
import com.tsky.yyj.entity.Classify;
import com.tsky.yyj.entity.Product;
import com.tsky.yyj.entity.Recharge;
import com.tsky.yyj.entity.Seller;
import com.tsky.yyj.entity.Store;

/**
 * 工具类：Json数据格式化
 * @author: cainiao
 * @version 1.0 
 * @time: 2010-3-10 上午06:03:36
 */
public final class YYJJsonFormat {
	public static Object sellersToJson(List<?> resultList, String sellerid) {
		// TODO Auto-generated method stub
		List mapList = new ArrayList();
		Map selMap = new HashMap();
		selMap.put("id", "");
		selMap.put("name", "--请选择--");
		if(BeanUtil.isEmpty(sellerid)){
			selMap.put("selected", true);
		}
		
		mapList.add(selMap);
		for (Object object : resultList) {
			Seller entity = (Seller) object;
			Map cellMap = new HashMap();
			
			String id = entity.getId();
			if(id.equals(sellerid)){
				cellMap.put("selected", "true");	
			}
			cellMap.put("id", id);
			cellMap.put("name", entity.getName());
			
			mapList.add(cellMap);
		}
	
		// Object object = JSONArray.fromObject(mapList);
		String object = null;
		try {
			object = JSONUtil.serialize(mapList);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return object;
	}

	public static String typesToJson(List resultList, String roleid) {
		List mapList = new ArrayList();
		Map selMap = new HashMap();
		selMap.put("id", "");
		selMap.put("name", "--请选择--");
		if(BeanUtil.isEmpty(roleid)){
			selMap.put("selected", true);
		}
		
		mapList.add(selMap);
		for (Object object : resultList) {
			Classify entity = (Classify) object;
			Map cellMap = new HashMap();
			
			String id = entity.getId();
			if(id.equals(roleid)){
				cellMap.put("selected", "true");	
			}
			cellMap.put("id", id);
			cellMap.put("name", entity.getName());
			
			mapList.add(cellMap);
		}
	
		// Object object = JSONArray.fromObject(mapList);
		String object = null;
		try {
			object = JSONUtil.serialize(mapList);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return object;
	}

	public static Object citysToJson(List<?> resultList, String sellerid) {
		// TODO Auto-generated method stub
		List mapList = new ArrayList();
		Map selMap = new HashMap();
		selMap.put("id", "");
		selMap.put("name", "--请选择--");
		if(BeanUtil.isEmpty(sellerid)){
			selMap.put("selected", true);
		}
		
		mapList.add(selMap);
		for (Object object : resultList) {
			Constant entity = (Constant) object;
			Map cellMap = new HashMap();
			
			String id = entity.getId();
			if(id.equals(sellerid)){
				cellMap.put("selected", "true");	
			}
			cellMap.put("id", id);
			cellMap.put("name", entity.getName());
			
			mapList.add(cellMap);
		}
	
		// Object object = JSONArray.fromObject(mapList);
		String object = null;
		try {
			object = JSONUtil.serialize(mapList);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return object;
	}
	
	public static Object areasToJson(List<?> resultList, String areaid) {
		// TODO Auto-generated method stub
		List mapList = new ArrayList();
		Map selMap = new HashMap();
		selMap.put("id", "");
		selMap.put("name", "--请选择--");
		if(BeanUtil.isEmpty(areaid)){
			selMap.put("selected", true);
		}
		
		mapList.add(selMap);
		for (Object object : resultList) {
			Area entity = (Area) object;
			Map cellMap = new HashMap();
			
			String id = entity.getId();
			if(id.equals(areaid)){
				cellMap.put("selected", "true");	
			}
			cellMap.put("id", id);
			cellMap.put("name", entity.getName());
			
			mapList.add(cellMap);
		}
	
		// Object object = JSONArray.fromObject(mapList);
		String object = null;
		try {
			object = JSONUtil.serialize(mapList);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return object;
	}

	/**
	 * User实体对象格式化为JSON数据
	 * @param entityList
	 * @param page
	 * @param total
	 * @return
	 */
	public static String areaToJson(QueryBean resultList) {
		Map pageInfo = new HashMap();
		pageInfo.put("total", resultList.getTotal());
		
		List entityList = resultList.getDataList();
		
		List mapList = new ArrayList();
		for (Object object : entityList) {
			Area entity = (Area) object;
			Constant city = entity.getCity();
			Map cellMap = new HashMap();
			
			cellMap.put("id", entity.getId());
			cellMap.put("name", entity.getName());
			cellMap.put("city", city == null? "": city.getShowValue());
			
			mapList.add(cellMap);
		}

		pageInfo.put("rows", mapList);
		
		String object = null;
		try {
			object = JSONUtil.serialize(pageInfo);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return object;
	}
	
	public static String classifyToJson(QueryBean resultList) {
		Map pageInfo = new HashMap();
		pageInfo.put("total", resultList.getTotal());
		
		List entityList = resultList.getDataList();
		
		List mapList = new ArrayList();
		for (Object object : entityList) {
			Classify entity = (Classify) object;
			Classify pid = entity.getParent();
			Map cellMap = new HashMap();
			
			cellMap.put("id", entity.getId());
			cellMap.put("name", pid == null? "": entity.getName());
			cellMap.put("parent", pid == null? entity.getName(): pid.getName());
			
			mapList.add(cellMap);
		}

		pageInfo.put("rows", mapList);
		
		String object = null;
		try {
			object = JSONUtil.serialize(pageInfo);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return object;
	}

	public static String sellerToJson(QueryBean resultList) {
		Map pageInfo = new HashMap();
		pageInfo.put("total", resultList.getTotal());
		
		List entityList = resultList.getDataList();
		
		List mapList = new ArrayList();
		for (Object object : entityList) {
			Seller entity = (Seller) object;
			Map cellMap = new HashMap();
			
			cellMap.put("id", entity.getId());
			cellMap.put("name", entity.getName());
			cellMap.put("code", entity.getCode());
			cellMap.put("contact", entity.getContact());
			
			mapList.add(cellMap);
		}

		pageInfo.put("rows", mapList);
		
		String object = null;
		try {
			object = JSONUtil.serialize(pageInfo);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return object;
	}
	
	public static String storeToJson(QueryBean resultList) {
		Map pageInfo = new HashMap();
		pageInfo.put("total", resultList.getTotal());
		
		List entityList = resultList.getDataList();
		
		List mapList = new ArrayList();
		for (Object object : entityList) {
			Store entity = (Store) object;
			Seller seller = entity.getSeller();
			Area area = entity.getArea();
			Map cellMap = new HashMap();
			
			cellMap.put("id", entity.getId());
			cellMap.put("name", entity.getName());
			cellMap.put("area", area == null? "": area.getCity().getName() + area.getName());
			cellMap.put("address", entity.getAddress());
			cellMap.put("seller", seller == null? "": seller.getName());
			cellMap.put("status", "0".equals(entity.getStatus()) ? "待审核": "已审核");
			
			mapList.add(cellMap);
		}

		pageInfo.put("rows", mapList);
		
		String object = null;
		try {
			object = JSONUtil.serialize(pageInfo);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return object;
	}

	public static String agentToJson(QueryBean resultList) {
		// TODO Auto-generated method stub
		Map pageInfo = new HashMap();
		pageInfo.put("total", resultList.getTotal());
		
		List entityList = resultList.getDataList();
		
		List mapList = new ArrayList();
		for (Object object : entityList) {
			Agent entity = (Agent) object;
			Map cellMap = new HashMap();
			
			cellMap.put("id", entity.getId());
			cellMap.put("name", entity.getName());
			cellMap.put("code", entity.getCode());
			cellMap.put("email", entity.getEmail());
			cellMap.put("idCode", entity.getIdCode());
			
			mapList.add(cellMap);
		}

		pageInfo.put("rows", mapList);
		
		String object = null;
		try {
			object = JSONUtil.serialize(pageInfo);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return object;
	}

	public static String rechargeToJson(QueryBean resultList) {
		// TODO Auto-generated method stub
		Map pageInfo = new HashMap();
		pageInfo.put("total", resultList.getTotal());
		
		List entityList = resultList.getDataList();
		
		List mapList = new ArrayList();
		for (Object object : entityList) {
			Recharge entity = (Recharge) object;
			Map cellMap = new HashMap();
			
			cellMap.put("id", entity.getId());
			cellMap.put("name", entity.getName());
			cellMap.put("execDate", BeanUtil.formatToDate(entity.getExecDate()));
			cellMap.put("money", entity.getMoney());
			
			mapList.add(cellMap);
		}

		pageInfo.put("rows", mapList);
		
		String object = null;
		try {
			object = JSONUtil.serialize(pageInfo);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return object;
	}

	public static String productToJson(QueryBean resultList) {
		// TODO Auto-generated method stub
		Map pageInfo = new HashMap();
		pageInfo.put("total", resultList.getTotal());
		
		List entityList = resultList.getDataList();
		
		List mapList = new ArrayList();
		for (Object object : entityList) {
			Product entity = (Product) object;
			Store store = entity.getStore();
			Map cellMap = new HashMap();
			
			cellMap.put("id", entity.getId());
			cellMap.put("title", entity.getTitle());
			cellMap.put("beginDate", entity.getBeginDate());
			cellMap.put("endData", entity.getEndDate());
			cellMap.put("price", entity.getPrice());
			cellMap.put("store", store.getName());
			cellMap.put("area", store.getArea().getName());
			cellMap.put("status", entity.getStatus());
			
			mapList.add(cellMap);
		}

		pageInfo.put("rows", mapList);
		
		String object = null;
		try {
			object = JSONUtil.serialize(pageInfo);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return object;
	}
	
}
